stringlist: Remove n_additions argument from gtk_string_list_splice()
authorBenjamin Otte <otte@redhat.com>
Tue, 30 Jun 2020 17:36:51 +0000 (19:36 +0200)
committerBenjamin Otte <otte@redhat.com>
Sun, 5 Jul 2020 00:59:21 +0000 (02:59 +0200)
char ** arrays are null-terminated everywhere, so make sure they are in
splice(), too.

Also fix the argument to be a const char * const * like in the
constructor.

gtk/gtkstringlist.c
gtk/gtkstringlist.h
testsuite/gtk/stringlist.c

index e58a0fe6b690c4f1b24f298b098f69fcfb622b24..a3e2af5f68c4cebbb25043bff4ef17eef89ada5d 100644 (file)
@@ -449,11 +449,10 @@ gtk_string_list_new (const char * const *strings)
  * @self: a #GtkStringList
  * @position: the position at which to make the change
  * @n_removals: the number of strings to remove
- * @additions: (array length=n_additions): the strings to add
- * @n_additions: the number of items to add
+ * @additions: (array zero-terminated=1) (nullable): The strings to add
  *
- * Changes @self by removing @n_removals strings and adding @n_additions
- * strings to it.
+ * Changes @self by removing @n_removals strings and adding @additions
+ * to it.
  *
  * This function is more efficient than gtk_string_list_insert() and
  * gtk_string_list_remove(), because it only emits
@@ -466,14 +465,13 @@ gtk_string_list_new (const char * const *strings)
  * of the list at the time this function is called).
  */
 void
-gtk_string_list_splice (GtkStringList  *self,
-                        guint           position,
-                        guint           n_removals,
-                        const char    **additions,
-                        guint           n_additions)
+gtk_string_list_splice (GtkStringList      *self,
+                        guint               position,
+                        guint               n_removals,
+                        const char * const *additions)
 {
   GSequenceIter *it;
-  guint n_items;
+  guint add, n_items;
 
   g_return_if_fail (GTK_IS_STRING_LIST (self));
   g_return_if_fail (position + n_removals >= position); /* overflow */
@@ -493,17 +491,18 @@ gtk_string_list_splice (GtkStringList  *self,
       it = end;
     }
 
-  if (n_additions)
+  if (additions)
     {
-      gint i;
-
-      for (i = 0; i < n_additions; i++)
+      for (add = 0; additions[add]; add++)
         {
-          g_sequence_insert_before (it, gtk_string_object_new (additions[i]));
+          g_sequence_insert_before (it, gtk_string_object_new (additions[add]));
         }
     }
+  else
+    add = 0;
 
-  g_list_model_items_changed (G_LIST_MODEL (self), position, n_removals, n_additions);
+  if (n_removals || add)
+    g_list_model_items_changed (G_LIST_MODEL (self), position, n_removals, add);
 }
 
 /**
index 0a29612ef88ee9641164943385b81665d7e5356c..17be87864b7c1df797cf6aaeb67273a6ed96b0d6 100644 (file)
@@ -45,30 +45,29 @@ GDK_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (GtkStringList, gtk_string_list, GTK, STRING_LIST, GObject)
 
 GDK_AVAILABLE_IN_ALL
-GtkStringList * gtk_string_list_new             (const char * const *strings);
+GtkStringList * gtk_string_list_new             (const char * const    *strings);
 
 GDK_AVAILABLE_IN_ALL
-void            gtk_string_list_append          (GtkStringList *self,
-                                                 const char    *string);
+void            gtk_string_list_append          (GtkStringList         *self,
+                                                 const char            *string);
 
 GDK_AVAILABLE_IN_ALL
-void            gtk_string_list_take            (GtkStringList *self,
-                                                 char          *string);
+void            gtk_string_list_take            (GtkStringList         *self,
+                                                 char                  *string);
 
 GDK_AVAILABLE_IN_ALL
-void            gtk_string_list_remove          (GtkStringList *self,
-                                                 guint          position);
+void            gtk_string_list_remove          (GtkStringList         *self,
+                                                 guint                  position);
 
 GDK_AVAILABLE_IN_ALL
-void            gtk_string_list_splice          (GtkStringList  *self,
-                                                 guint           position,
-                                                 guint           n_removals,
-                                                 const char    **additions,
-                                                 guint           n_additions);
+void            gtk_string_list_splice          (GtkStringList         *self,
+                                                 guint                  position,
+                                                 guint                  n_removals,
+                                                 const char * const    *additions);
 
 GDK_AVAILABLE_IN_ALL
-const char *    gtk_string_list_get_string      (GtkStringList *self,
-                                                 guint          position);
+const char *    gtk_string_list_get_string      (GtkStringList         *self,
+                                                 guint                  position);
 
 G_END_DECLS
 
index 5b6256f93f3b8a06ac74011704e18b6d4ec3770b..12169be13e33973d2e55ccac8bcd59c30a75c9e0 100644 (file)
@@ -184,7 +184,7 @@ test_splice (void)
 
   assert_model (list, "a b c d e");
 
-  gtk_string_list_splice (list, 2, 2, (const char *[]){ "x", "y", "z" }, 3);
+  gtk_string_list_splice (list, 2, 2, (const char *[]){ "x", "y", "z", NULL });
 
   assert_model (list, "a b x y z e");
   assert_changes (list, "2-2+3");